package josema.org.influenciasolar;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class ResultActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result);

		// Receive extras
        Bundle extras = getIntent().getExtras();
        String inputDate = extras.get("inputDate").toString();

        Toast.makeText(this, inputDate, Toast.LENGTH_SHORT)
        .show();    		
	
		
		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		Date date = new Date();
		try {
			date = df.parse(inputDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		float birthday = GetBirthday(date);
		float years = GetYears(date);
		float decade = GetDecade(date);
		float specificday = GetSpecificDay(date);

		DrawCircleResult(birthday, years, decade, specificday);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_result, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.itemBackResult:
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}

		return true;
	}
	
	private float GetBirthday (Date date){
		return (float) 0.4;
	}
	
	private float GetYears (Date date){
		return 1;
	}
	
	private float GetDecade (Date date){
		return 3;
	}
	
	private float GetSpecificDay (Date date){
		return (float) 4.4;
	}

	private void DrawCircleResult(float birthday, float years, float decade, float specificday) {
		// Draw result in circle
		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
		Lienzo fondo = new Lienzo(this, birthday, years, decade, specificday);
		linearLayout.addView(fondo);
	}

	class Lienzo extends View {

		private float _birthday;
		private float _years;
		private float _decade;
		private float _specificday;

		public Lienzo(Context context, float lineBirthday, float lineYears,
				float lineDecade, float lineSpecificDay) {
			super(context);

			// Save lines
			_birthday = lineBirthday;
			_years = lineYears;
			_decade = lineDecade;
			_specificday = lineSpecificDay;

		}

		protected void onDraw(Canvas canvas) {
			// Get colors from Resources
			int colorCircle = getResources().getColor(R.color.colorCircle);
			int colorA = getResources().getColor(R.color.colorA);
			int colorB = getResources().getColor(R.color.colorB);
			int colorC = getResources().getColor(R.color.colorC);
			int colorD = getResources().getColor(R.color.colorD);


			// Define Canvas and Paint
			canvas.drawColor(R.color.bgrColor);
			Paint p = new Paint();

			// smooths
			p.setAntiAlias(true);
			p.setColor(colorCircle);
			p.setStyle(Paint.Style.STROKE);
			p.setStrokeWidth(5);

			// Define area
			RectF rectF = new RectF(20, 20, 300, 300);
			canvas.drawOval(rectF, p);

			// First: Draw line birthday
			p.setColor(colorA);
			canvas.drawArc(rectF, _birthday * 30 - 272, 4, true, p);
			canvas.drawArc(rectF, _birthday * 30 - 90, 180, true, p);

			// Second: Draw line of years
			p.setColor(colorB);
			canvas.drawArc(rectF, _years * 30 - 272, 4, true, p);
			canvas.drawArc(rectF, _years * 30 - 90, 180, true, p);

			// Third: Draw decade
			p.setColor(colorC);
			canvas.drawArc(rectF, _decade * 30 - 272, 4, true, p);
			canvas.drawArc(rectF, _decade * 30 - 90, 180, true, p);

			// Four: [Optional] Specific Day
			if ( _specificday != -1 )
			{
				p.setColor(colorD);
				canvas.drawArc(rectF, _specificday * 30 - 272, 4, true, p);	
				canvas.drawArc(rectF, _specificday * 30 - 90, 180, true, p);	
			}
			
			// Finish: Draw Circle
			p.setColor(colorCircle);
			canvas.drawOval(rectF, p);
			

		}
	}
}
